package com.rongzhe.house.manager;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.rongzhe.house.entity.ResponseEntityVo;
import com.rongzhe.house.entity.SubwayInfo;
import com.rongzhe.house.entity.voo.City;
import com.rongzhe.house.entity.voo.Country;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.internet.InternetRequestWorker;
import com.rongzhe.house.internet.ResponseDispatcher;
import com.rongzhe.house.internet.net.PositionInterface;
import com.rongzhe.house.service.LocationService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huang on 2017/11/26.
 */

public class PositionManager {
    private static final String DEFAULT_CITY_ID = "5001";
    private BDLocation mBDLocation;

    private City mCurrentCity;
    private List<City> mInServiceCity;
    private List<BDLocationChangedListener> mListeners = new ArrayList<>();

    public interface BDLocationChangedListener {
        void onBDLocationChanged();
    }

    private PositionManager() {
    }
    private static PositionManager sInstance;
    public static PositionManager getInstance() {
        if (sInstance == null) {
            sInstance = new PositionManager();
        }
        return sInstance;
    }

    public BDLocation getBDLocation() {
        return mBDLocation;
    }

    public void setBDLocation(BDLocation bdLocation) {
        this.mBDLocation = bdLocation;
        for (BDLocationChangedListener listener : mListeners) {
            listener.onBDLocationChanged();
        }
    }

    public void addBDLocationChangedListenr(BDLocationChangedListener bdLocationListener) {
        mListeners.add(bdLocationListener);
    }

    public void removeBDLocationChangedListenr(BDLocationChangedListener bdLocationListener) {
        mListeners.remove(bdLocationListener);
    }

    public City getCurrentCity() {
        return mCurrentCity;
    }

    public String getCurrentCityId() {
        return mCurrentCity == null ? DEFAULT_CITY_ID : mCurrentCity.getCityId();
    }

    public void getLocationInfo(Context context) {
        Context applicationContext = context.getApplicationContext();
        loadAllCity(null);
        applicationContext.startService(new Intent(context, LocationService.class));
    }

    public City getCityFromName(String name) {
        if (mInServiceCity == null) {
            return null;
        }
        for (City city : mInServiceCity) {
            if (TextUtils.equals(name, city.getCityName())) {
                return city;
            }
        }
        return null;
    }

    public void loadAllCity(DataListener<List<City>> dataListener) {
        if (mInServiceCity != null && dataListener != null) {
            dataListener.onSuccess(mInServiceCity, null);
            return;
        }
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                .create(PositionInterface.class).getAllCity(),
                new ResponseDispatcher(dataListener) {
                    @Override
                    protected void handleSuccess(ResponseEntityVo data) {
                        if (data.success() && data.getData() != null) {
                            mInServiceCity = (List<City>) data.getData();
                        }
                    }
                });
    }

    public void loadCountryList(String cityId,
                                DataListener<List<Country>> dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                .create(PositionInterface.class).getCityCountry(cityId),
                new ResponseDispatcher(dataListener));
    }

    public void loadSubwayInfo(String cityId, DataListener<List<SubwayInfo>> dataListener) {
        InternetRequestWorker.getInstance().asyncNetwork(InternetRequestWorker.getInstance()
                .create(PositionInterface.class).getSubwayInfo(cityId),
                new ResponseDispatcher(dataListener));
    }

    public void setCurrentCity(City city) {
        this.mCurrentCity = city;
    }
}
